const app = getApp()
const WXAPI = require('../../wxapi/main')
Page({

  data: {
    theme: '',
    mode: '', // 模式(care：关怀模式)
    userInfo: {},
    partUserInfo: {},
    showPhone: '',
    avatarUrl: '',
    ApiBaseUrl: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    let that = this;
    // let _partUserInfo = wx.getStorageSync('partUserInfo')    
    // if (!_partUserInfo) {
    //   app.goLoginPageTimeOut()
    // } else {
    //   that.setData({
    //     partUserInfo: _partUserInfo,
    //     showPhone: _partUserInfo.phone.replace(_partUserInfo.phone.substring(3,7), '****')
    //   })
    // }   
    // let userInfo = wx.getStorageSync('userInfo')    
    // if (userInfo) {
    //   that.setData({
    //     userInfo: userInfo
    //   })
    // }  
    that.setData({
      ApiBaseUrl: app.globalApi.baseUrl,
    })
    // 禁止转发
    wx.hideShareMenu()
  },
  onChooseAvatar(e) {
    let that = this
    console.log('current openId ', that.data.userInfo.openId)
    const { avatarUrl } = e.detail 
    let _openId = that.data.userInfo.openId
    console.log('avatar url', avatarUrl)    
    that.setData({
      avatarUrl,
    })
    let postData = {openId: _openId, avatarUrl: avatarUrl}
    WXAPI.updateAvatar(postData).then( res => {
      console.log(res)
      if (res.code == 0) {
        console.log(res.data)
        // wx.showToast({
        //   title: '已成功受理，稍后查看',
        // })
        wx.downloadFile({
          url: avatarUrl,
          success(res){      
            if (res.statusCode === 200) {
              console.log('download success');              
              let tempFilePath = res.tempFilePath
              console.log("获取到用户头像tempFilePath："+ tempFilePath)
              console.log('upload ', that.data.ApiBaseUrl + '/upload')
              wx.uploadFile({
                url: that.data.ApiBaseUrl + '/upload', //仅为示例，非真实的接口地址
                filePath: tempFilePath,
                header: {"Content-Type": "multipart/form-data" },
                name: 'file',
                formData: {
                  'openId': that.data.userInfo.openId,
                  'prefix': that.data.ApiBaseUrl
                },
                success (res){    
                  console.log('upload success',res);;                  
                },
                fail (err) {
                  console.log('upload fail', err)
                }
              })
            }
          }
        })
      } else {
        wx.showToast({
          title: '有问题',
        })
      }
    })

  },
  changeAvatar: function() {
    wx.navigateTo({
      url: '/pages/my/avatar',
    })
  },
  changeNickName: function() {
    wx.navigateTo({
      url: '/pages/my/nick',
    })
  },

  loadUserInfo: function() {
    let that = this
    let skey = wx.getStorageSync('loginFlag')
    if (skey) {
      WXAPI.loadUser({skey})
      .then(res => {
        console.log('res ', res)
        if (res.code === 0) {
          let tmpUser = res.data
          console.log('usr data ', tmpUser)
          that.setData({
            userInfo: tmpUser,
            avatarUrl: tmpUser.avatar
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    let _partUserInfo = wx.getStorageSync('partUserInfo')    
    if (!_partUserInfo) {
      app.goLoginPageTimeOut()
    } else {
      that.setData({
        partUserInfo: _partUserInfo,
        showPhone: _partUserInfo.phone.replace(_partUserInfo.phone.substring(3,7), '****')        
      })
      that.loadUserInfo()
    }   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})